#include <scene.h>

#include <object.h>
#include <line.h>
#include <circle.h>
#include <ellipse.h>
#include <polygon.h>

#include <visual.h>
#include <surface.h>

#include <unittest.h>

/**
 * DESC: Tests scene rendering
 * IMP: HIGH
 */

int main( int argc, char *argv[] )
{
  visual_init( 800, 600 );

  coord_t pos = { 0, 0 };
  surface_t *surface = surface_new( 800, 600 );
  assert_not_null( surface );

  scene_t *scene;

  uint16_t depth = 0;

  scene = scene_new();
  assert_not_null( scene );

  color_t color;

  line_t *line = line_new();
  COLOR_FROM_ARGB( color, COLOR_RED );
  object_init4( OBJECT( line ), color, 0.5, depth++ );
  SET_RECT( line->r, 0, 0, 800, 600 );
  scene_add_object( scene, OBJECT( line ) );

  circle_t *circle = circle_new();
  COLOR_FROM_ARGB( color, COLOR_GREEN );
  object_init4( OBJECT( circle ), color, 0.5, depth++ );
  SET_COORD( circle->center, 400, 300 );
  circle->radius = 100;
  scene_add_object( scene, OBJECT( circle ) );

  ellipse_t *ellipse = ellipse_new();
  COLOR_FROM_ARGB( color, COLOR_BLUE );
  object_init4( OBJECT( ellipse ), color, 0.5, depth++ );
  SET_COORD( ellipse->center, 400, 300 );
  ellipse->radiusx = 200;
  ellipse->radiusy =  50;
  scene_add_object( scene, OBJECT( ellipse ) );

  polygon_t *polygon = polygon_new();
  COLOR_FROM_ARGB( color, COLOR_YELLOW );
  object_init4( polygon, color, 0.5, depth++ );
  polygon_add_vertex( polygon, 100, 100 );
  polygon_add_vertex( polygon, 700, 500 );
  polygon_add_vertex( polygon, 100, 500 );
  polygon_add_vertex( polygon, 700, 100 );
  scene_add_object( scene, OBJECT( polygon ) );

  scene_render( scene, surface );

  visual_display( surface, pos );
  visual_update();
  visual_waitkey();
  surface_clear( surface );

  OBJECT_SET_DRAW_OPT( line, DRAW_FILL );
  OBJECT_SET_DRAW_OPT( circle, DRAW_FILL );
  OBJECT_SET_DRAW_OPT( ellipse, DRAW_FILL );
  OBJECT_SET_DRAW_OPT( polygon, DRAW_FILL );

  scene_render( scene, surface );

  visual_display( surface, pos );
  visual_update();
  visual_waitkey();
  surface_clear( surface );


  OBJECT_UNSET_DRAW_OPT( line, DRAW_FILL );
  OBJECT_UNSET_DRAW_OPT( circle, DRAW_FILL );
  OBJECT_UNSET_DRAW_OPT( ellipse, DRAW_FILL );
  OBJECT_UNSET_DRAW_OPT( polygon, DRAW_FILL );

  OBJECT_SET_DRAW_OPT( line, DRAW_ANTIALIAS );
  OBJECT_SET_DRAW_OPT( circle, DRAW_ANTIALIAS );
  OBJECT_SET_DRAW_OPT( ellipse, DRAW_ANTIALIAS );
  OBJECT_SET_DRAW_OPT( polygon, DRAW_ANTIALIAS );

  scene_render( scene, surface );

  visual_display( surface, pos );
  visual_update();
  visual_waitkey();
  surface_clear( surface );

  OBJECT_SET_DRAW_OPT( line, DRAW_FILL );
  OBJECT_SET_DRAW_OPT( circle, DRAW_FILL );
  OBJECT_SET_DRAW_OPT( ellipse, DRAW_FILL );
  OBJECT_SET_DRAW_OPT( polygon, DRAW_FILL );

  scene_render( scene, surface );

  visual_display( surface, pos );
  visual_update();
  visual_waitkey();
  surface_clear( surface );


  surface_free( surface );

  scene_free( scene );

  object_free( line );
  object_free( circle );
  object_free( ellipse );
  object_free( polygon );

  visual_quit();

  return 0;
}
